<template>
	<div class="login-wrap">
		<div class="ms-title">综合平台</div>
		
		<div class="ms-login">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
				<!--<el-form-item prop="companyId" label="公司">
					<el-select v-model="ruleForm.companyId" placeholder="请选择" clearable style='width:100%'>
                        <el-option v-for="item in companyDate" :key="item.company_id" :label="item.company_name" :value="item.company_id"></el-option>
                    </el-select>
				</el-form-item>-->
				<el-form-item prop="username" label="用户名">
					<el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
				</el-form-item>
				<el-form-item prop="password" label="密码">
					<el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
				</div>
				<!--<p style="font-size:12px;line-height:30px;color:#999;">Tips : 初始用户名与密码都是手机号码。</p>-->
			</el-form>
		</div>
	</div>
</template>

<script>
	import { login, apiparas, query,pub_query } from '../../api/api';
	import { Cookie } from '../../js/common';
	export default {
		data: function() {
			return {
				companyDate:[],
				ruleForm: {
					companyId:'EI',
					username: '',
					password: ''
				},
				rules: {
//					companyId: [{
//						required: true,
//						message: '请输入公司ID',
//						trigger: 'blur'
//					}],
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			submitForm(formName) {

				let paras = {
					user: this.ruleForm.username,
					pwd: this.ruleForm.password,
					company:this.ruleForm.companyId
				};
				console.log(paras,'paras');
				login(paras).then(response => {
						console.log(response,'response');
					if(response.errid == 0) { //错误信息(如果返回错误信息为空，则认证通过)
						Cookie.Set("token", response.token, 0);
						Cookie.Set("user_key", response.user_key, 0);
						Cookie.Set("user_name", response.user_name, 0);
						Cookie.Set("company_key", response.company_key, 0);
						Cookie.Set("company_name", response.company_name, 0);
						Cookie.Set("datasource", response.datasource, 0);
						// localStorage.setItem('ms_username', response.uname);
						//window.location.href="/";
						this.$router.push('/md/welcome'); //登录成功，进入系统首面
					} else {
						this.$notify.error({title: '错误',message:'用户名或密码错误' ,type:'error'});
					}
				}).catch(res => {
					this.$notify.error({title: '错误',message:'网络错误,请重新刷新',type:'error'});
				});
			},
//			getCompany: function() { //查询数据
//				//显示数据
//				let _paras = apiparas.getPubParas('default','get_company');
//				pub_query(_paras).then(response => {
//					let json_data = response.result;
//					this.companyDate = json_data;
//				});
//			},
		},
		created: function() {
			//this.getCompany();
		}
	}
</script>

<style scoped>
	.login-wrap {
		position: relative;
		width: 100%;
		height: 100%;
	}
	
	.ms-title {
		position: absolute;
		top: 50%;
		width: 100%;
		margin-top: -230px;
		text-align: center;
		font-size: 30px;
		color: #fff;
	}
	
	.ms-login {
		position: absolute;
		left: 35%;
		top: 50%;
		width: 30%;
		height: 180px;
		margin: -150px 0 0 -20px;
		padding: 40px;
		border-radius: 5px;
		background: #fff;
	}
	
	.login-btn {
		text-align: center;
	}
	
	.login-btn button {
		width: 100%;
		height: 36px;
	}
</style>